﻿using System.Windows.Forms;
using System.Drawing;
using System;

namespace DotNet.Common
{
    internal class GapControl : Label
    {
        public override Size MinimumSize
        {
            get
            {
                Size minimumSize;
                using (Graphics g = Graphics.FromHwnd(Handle))
                {
                    minimumSize = TextRenderer.MeasureText(g,
                       base.Text, Font, Size,
                       TextFormatFlags.HorizontalCenter | TextFormatFlags.SingleLine | TextFormatFlags.NoPadding);
                }
                return minimumSize;
             }
        }

        public GapControl(string _gapText)
        {
            base.Text = _gapText;
            base.Size = MinimumSize;
            base.TextAlign = ContentAlignment.MiddleLeft;
            base.FlatStyle = FlatStyle.System;
        }

        protected override void OnFontChanged(EventArgs e)
        {
            base.OnFontChanged(e);
            base.Size = this.MinimumSize;
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            base.Size = this.MinimumSize;
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            base.Size = this.MinimumSize;
        }

        protected override void OnParentForeColorChanged(EventArgs e)
        {
            base.OnParentForeColorChanged(e);
            base.ForeColor = Parent.ForeColor;
        }
    }
}
